<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.example.domain.User" %>
<%@ page import="com.example.dao.OrderDao" %>
<%@ page import="com.example.domain.Order" %>
<%@ page import="com.example.domain.OrderItem" %>
<%@ page import="java.util.List" %>
<html>
<head>
    <title>订单详情 - 后台管理</title>
    <link rel="stylesheet" href="../assets/css/styles.css">
</head>
<body>
    <%
        // 检查用户是否已登录
        User currentUser = (User) session.getAttribute("user");
        if (currentUser == null) {
            response.sendRedirect("../auth/login.jsp");
            return;
        }
        
        // 获取订单ID
        String orderIdParam = request.getParameter("id");
        if (orderIdParam == null || orderIdParam.isEmpty()) {
            response.sendRedirect("user_info.jsp");
            return;
        }
        
        int orderId = Integer.parseInt(orderIdParam);
    %>
    
    <!-- 导航栏 -->
    <nav class="header-nav">
        <div class="nav-container">
            <a href="../index.jsp" class="nav-brand">购物网</a>
            
            <ul class="nav-links">
                <li><a href="../index.jsp">首页</a></li>
                <li><a href="../products/list.jsp">商品浏览</a></li>
                <li><a href="../cart/view/cart.jsp">购物车</a></li>
                <li><a href="user_info.jsp" class="active">用户中心</a></li>
            </ul>
            
            <div class="user-actions">
                <span class="user-info">欢迎, <%= currentUser.getUsername() %>!</span>
                <a href="../auth/logout.jsp" class="btn btn-outline">退出</a>
            </div>
        </div>
    </nav>
    
    <div class="container">
        <div class="page-title">
            <h1>订单详情</h1>
        </div>
        
        <%
            try {
                OrderDao orderDao = new OrderDao();
                Order order = orderDao.getOrderById(orderId);
                
                // 检查订单是否存在且属于当前用户
                if (order == null || order.getUser_id() != currentUser.getId()) {
        %>
        <div class="card" style="padding: 3rem; text-align: center;">
            <h3>订单不存在</h3>
            <p style="margin: 1rem 0;">找不到指定的订单</p>
            <a href="user_info.jsp" class="btn">返回用户中心</a>
        </div>
        <%
                } else {
                    List<OrderItem> orderItems = orderDao.getOrderItemsByOrderId(orderId);
                    String statusText = "";
                    switch (order.getStatus()) {
                        case 0: statusText = "待支付"; break;
                        case 1: statusText = "已支付"; break;
                        case 2: statusText = "已发货"; break;
                        case 3: statusText = "已完成"; break;
                        case 4: statusText = "已取消"; break;
                        default: statusText = "未知状态";
                    }
        %>
        
        <div class="card">
            <h3>订单信息</h3>
            <div class="order-details">
                <div class="form-group">
                    <label>订单号:</label>
                    <span><%= order.getOrder_no() != null ? order.getOrder_no() : "N/A" %></span>
                </div>
                <div class="form-group">
                    <label>订单状态:</label>
                    <span><%= statusText %></span>
                </div>
                <div class="form-group">
                    <label>总金额:</label>
                    <span>¥<%= String.format("%.2f", order.getTotal_amount()) %></span>
                </div>
                <div class="form-group">
                    <label>收货人:</label>
                    <span><%= order.getReceiver_name() != null ? order.getReceiver_name() : "N/A" %></span>
                </div>
                <div class="form-group">
                    <label>联系电话:</label>
                    <span><%= order.getReceiver_phone() != null ? order.getReceiver_phone() : "N/A" %></span>
                </div>
                <div class="form-group">
                    <label>收货地址:</label>
                    <span><%= order.getReceiver_address() != null ? order.getReceiver_address() : "N/A" %></span>
                </div>
                <div class="form-group">
                    <label>下单时间:</label>
                    <span><%= order.getCreated_at() != null ? order.getCreated_at().toString() : "N/A" %></span>
                </div>
            </div>
        </div>
        
        <div class="card">
            <h3>商品信息</h3>
            <table class="table">
                <thead>
                    <tr>
                        <th>商品名称</th>
                        <th>单价</th>
                        <th>数量</th>
                        <th>小计</th>
                    </tr>
                </thead>
                <tbody>
                    <%
                        for (OrderItem item : orderItems) {
                    %>
                    <tr>
                        <td><%= item.getProduct_name() %></td>
                        <td>¥<%= String.format("%.2f", item.getPrice()) %></td>
                        <td><%= item.getQuantity() %></td>
                        <td>¥<%= String.format("%.2f", item.getSubtotal()) %></td>
                    </tr>
                    <%
                        }
                    %>
                </tbody>
            </table>
        </div>
        
        <div style="text-align: center; margin-top: 2rem;">
            <a href="user_info.jsp" class="btn">返回用户中心</a>
        </div>
        <%
                }
            } catch (Exception e) {
        %>
        <div class="card" style="padding: 2rem; text-align: center; color: #dc3545;">
            <p>加载订单详情失败: <%= e.getMessage() %></p>
            <a href="user_info.jsp" class="btn">返回用户中心</a>
        </div>
        <%
            }
        %>
    </div>
</body>
</html>